package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.po.shop.ShopVisitPo;
import com.java110.mall.shop.bmo.shopVisit.IDeleteShopVisitBMO;
import com.java110.mall.shop.bmo.shopVisit.IGetShopVisitBMO;
import com.java110.mall.shop.bmo.shopVisit.ISaveShopVisitBMO;
import com.java110.mall.shop.bmo.shopVisit.IUpdateShopVisitBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/shopVisit")
public class ShopVisitApi {

    @Autowired
    private ISaveShopVisitBMO saveShopVisitBMOImpl;
    @Autowired
    private IUpdateShopVisitBMO updateShopVisitBMOImpl;
    @Autowired
    private IDeleteShopVisitBMO deleteShopVisitBMOImpl;

    @Autowired
    private IGetShopVisitBMO getShopVisitBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVisit/saveShopVisit
     * @path /app/shopVisit/saveShopVisit
     */
    @RequestMapping(value = "/saveShopVisit", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopVisit(HttpServletRequest request,@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");


        ShopVisitPo shopVisitPo = BeanConvertUtil.covertBean(reqJson, ShopVisitPo.class);
        shopVisitPo.setVisitIp(getIpAddress(request));
        return saveShopVisitBMOImpl.save(shopVisitPo);
    }

    private static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVisit/updateShopVisit
     * @path /app/shopVisit/updateShopVisit
     */
    @RequestMapping(value = "/updateShopVisit", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopVisit(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "visitId", "visitId不能为空");


        ShopVisitPo shopVisitPo = BeanConvertUtil.covertBean(reqJson, ShopVisitPo.class);
        return updateShopVisitBMOImpl.update(shopVisitPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVisit/deleteShopVisit
     * @path /app/shopVisit/deleteShopVisit
     */
    @RequestMapping(value = "/deleteShopVisit", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopVisit(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "visitId", "visitId不能为空");


        ShopVisitPo shopVisitPo = BeanConvertUtil.covertBean(reqJson, ShopVisitPo.class);
        return deleteShopVisitBMOImpl.delete(shopVisitPo);
    }

}
